package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PStoreProductLogBo;
import com.ruoyi.system.domain.vo.PStoreProductLogVo;
import com.ruoyi.system.domain.PStoreProductLog;
import com.ruoyi.system.mapper.PStoreProductLogMapper;
import com.ruoyi.system.service.IPStoreProductLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品日志Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-01
 */
@RequiredArgsConstructor
@Service
public class PStoreProductLogServiceImpl implements IPStoreProductLogService {

    private final PStoreProductLogMapper baseMapper;

    /**
     * 查询商品日志
     */
    @Override
    public PStoreProductLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品日志列表
     */
    @Override
    public TableDataInfo<PStoreProductLogVo> queryPageList(PStoreProductLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PStoreProductLog> lqw = buildQueryWrapper(bo);
        Page<PStoreProductLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品日志列表
     */
    @Override
    public List<PStoreProductLogVo> queryList(PStoreProductLogBo bo) {
        LambdaQueryWrapper<PStoreProductLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PStoreProductLog> buildQueryWrapper(PStoreProductLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PStoreProductLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), PStoreProductLog::getType, bo.getType());
        lqw.eq(bo.getProductId() != null, PStoreProductLog::getProductId, bo.getProductId());
        lqw.eq(bo.getUid() != null, PStoreProductLog::getUid, bo.getUid());
        lqw.eq(bo.getVisitNum() != null, PStoreProductLog::getVisitNum, bo.getVisitNum());
        lqw.eq(bo.getCartNum() != null, PStoreProductLog::getCartNum, bo.getCartNum());
        lqw.eq(bo.getOrderNum() != null, PStoreProductLog::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getPayNum() != null, PStoreProductLog::getPayNum, bo.getPayNum());
        lqw.eq(bo.getPayPrice() != null, PStoreProductLog::getPayPrice, bo.getPayPrice());
        lqw.eq(bo.getCostPrice() != null, PStoreProductLog::getCostPrice, bo.getCostPrice());
        lqw.eq(bo.getPayUid() != null, PStoreProductLog::getPayUid, bo.getPayUid());
        lqw.eq(bo.getRefundNum() != null, PStoreProductLog::getRefundNum, bo.getRefundNum());
        lqw.eq(bo.getRefundPrice() != null, PStoreProductLog::getRefundPrice, bo.getRefundPrice());
        lqw.eq(bo.getCollectNum() != null, PStoreProductLog::getCollectNum, bo.getCollectNum());
        lqw.eq(bo.getAddTime() != null, PStoreProductLog::getAddTime, bo.getAddTime());
        return lqw;
    }

    /**
     * 新增商品日志
     */
    @Override
    public Boolean insertByBo(PStoreProductLogBo bo) {
        PStoreProductLog add = BeanUtil.toBean(bo, PStoreProductLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品日志
     */
    @Override
    public Boolean updateByBo(PStoreProductLogBo bo) {
        PStoreProductLog update = BeanUtil.toBean(bo, PStoreProductLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PStoreProductLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
